package br.com.lh.urabuilder.business
{
	import br.com.lh.urabuilder.helper.MouseEventHelper;
	import br.com.lh.urabuilder.model.UraModelLocator;
	import br.com.lh.urabuilder.util.CoordinatesUtils;
	import br.com.lh.urabuilder.view.ActionComponent;
	import br.com.lh.urabuilder.view.RectangleSelectionView;
	
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.core.UIComponent;
	
	public class RectangleSelection
	{
		
		private var parent:Canvas;
		private var rectangleSelectionView:RectangleSelectionView;
		private var viewManager:ViewManager;
		private var mouseEventHelper:MouseEventHelper;
		private var preSelectedList:ArrayCollection = new ArrayCollection();
		private const OFFSET_CANVAS_Y:Number = 50;
		
		public function RectangleSelection(parent:UIComponent,viewManager:ViewManager,mouseEventHelper:MouseEventHelper){
			this.parent = parent as Canvas;
			this.viewManager = viewManager;
			this.mouseEventHelper = mouseEventHelper;
			this.rectangleSelectionView = new RectangleSelectionView(parent);
		}
		
		public function initRectangleSelection(event:MouseEvent):void{
			var uraModelLocator:UraModelLocator = UraModelLocator.getInstance();
			if (!mouseEventHelper.isRectangleAllowed()){
				return;
			}
			
			if (rectangleSelectionView.auxComponent !=null){
				endRectangleSelection(null);
				return;
			}
			if (!event.shiftKey){
				viewManager.selectAll(false);
				
			}
			preSelectedList = viewManager.getSelectedComponents();
        	rectangleSelectionView.initRectangleSelection(event);
           	parent.addEventListener(MouseEvent.MOUSE_MOVE,drawRectangleSelection,false,1);
           	parent.addEventListener(MouseEvent.MOUSE_UP,endRectangleSelection,true);
           	mouseEventHelper.isMouseDrawingRectangle = true;
          	
        }
       
        private function mouseLeft(event:MouseEvent):void{
        	endRectangleSelection(event);
        }
        
        public function drawRectangleSelection(event:MouseEvent):void{
        	if (notInCanvas(event)){
        		return;
        	}
        	var rect:Rectangle = rectangleSelectionView.drawRectangleSelection(event);
			var componentList:ArrayCollection = viewManager.getComponents();
			for each (var component:ActionComponent in componentList){
				var index:int = preSelectedList.getItemIndex(component);
				if (insideRectangle(rect,component)){
					if (index != -1){
						if (event.shiftKey){
							preSelectedList.removeItemAt(index);
						}
					}
					else{
						viewManager.selectComponent(component,true,true);
						trace ('selecting:' + component);						
					}
				}
				else{
					if (index < 0 || !event.shiftKey){
					viewManager.selectComponent(component,false,true);
					trace ('deselecting:' + component);
					}
				} 
			}
			event.stopImmediatePropagation();
        }
        
      	public function endRectangleSelection(event:MouseEvent):void{
 			rectangleSelectionView.endRectangleSelection();
        	parent.removeEventListener(MouseEvent.MOUSE_MOVE,drawRectangleSelection);
        	parent.removeEventListener(MouseEvent.MOUSE_UP,endRectangleSelection);
        	mouseEventHelper.isMouseDrawingRectangle = false;
      	}
		
		private function insideRectangle(rect:Rectangle,component:ActionComponent):Boolean{
			if ((component.x > rect.x && component.x < rect.x + rect.width) 
				||(rect.x > component.x && rect.x < component.x + component.width))
					{
				if ((component.y > rect.y && component.y < rect.y + rect.height) || 
							(rect.y > component.y && rect.y < component.y + component.height)){
					return true;
				}
			}
				
			return false;	
				
		}

		
		private function notInCanvas(event:MouseEvent):Boolean{
			var contentYToGlobal:int = CoordinatesUtils.getCanvasLocalToGlobalCoordinates().y;
        	return (event.stageY <=  contentYToGlobal  - parent.y || 
        			event.stageY >= contentYToGlobal + parent.height - OFFSET_CANVAS_Y + parent.verticalScrollPosition);
		}
		
		
	}
}